<?php

/**
 *      [Discuz!] (C)2001-2099 Comsenz Inc.
 *      This is NOT a freeware, use is subject to license terms
 *
 *      $Id: block_sort.php 7039 2010-03-28 07:56:51Z xupeng $
 */

if(!defined('IN_DISCUZ')) {
	exit('Access Denied');
}
class block_sort {
	var $setting = array();

	function block_sort(){
		$this->setting = array(
			'tids' => array(
				'title' => 'sortlist_tids',
				'type' => 'text'
			),
			'fids'	=> array(
				'title' => 'sortlist_fids',
				'type' => 'mselect',
				'value' => array()
			),
			'sortids' => array(
				'title' => 'sortlist_sortids',
				'type' => 'mradio',
				'value' => array()
			),
			'digest' => array(
				'title' => 'sortlist_digest',
				'type' => 'mcheckbox',
				'value' => array(
					array(1, 'sortlist_digest_1'),
					array(2, 'sortlist_digest_2'),
					array(3, 'sortlist_digest_3'),
					array(0, 'sortlist_digest_0')
				),
			),
			'stick' => array(
				'title' => 'sortlist_stick',
				'type' => 'mcheckbox',
				'value' => array(
					array(1, 'sortlist_stick_1'),
					array(2, 'sortlist_stick_2'),
					array(3, 'sortlist_stick_3'),
					array(0, 'sortlist_stick_0')
				),
			),
			'recommend' => array(
				'title' => 'sortlist_recommend',
				'type' => 'radio'
			),
			'orderby' => array(
				'title' => 'sortlist_orderby',
				'type'=> 'mradio',
				'value' => array(
					array('lastpost', 'sortlist_orderby_lastpost'),
					array('dateline', 'sortlist_orderby_dateline'),
					array('replies', 'sortlist_orderby_replies'),
					array('views', 'sortlist_orderby_views'),
					array('heats', 'sortlist_orderby_heats'),
					array('recommends', 'sortlist_orderby_recommends'),
					array('hourviews', 'sortlist_orderby_hourviews'),
					array('todayviews', 'sortlist_orderby_todayviews'),
					array('weekviews', 'sortlist_orderby_weekviews'),
					array('monthviews', 'sortlist_orderby_monthviews'),
				),
				'default' => 'lastpost'
			),
			'hours' => array(
				'title' => 'sortlist_orderby_hours',
				'type' => 'text'
			),
			'startrow' => array(
				'title' => 'sortlist_startrow',
				'type' => 'text',
				'default' => 0
			),
			'showitems' => array(
				'title' => 'sortlist_showitems',
				'type' => 'text',
				'default' => 10
			),
		);
	}

	function getsetting() {
		global $_G;
		$settings = $this->setting;

		if($settings['fids']) {
			loadcache('forums');
			$settings['fids']['value'][] = array(0, '&nbsp;');
			foreach($_G['cache']['forums'] as $fid => $forum) {
				$settings['fids']['value'][] = array($fid, ($forum['type'] == 'forum' ? str_repeat('&nbsp;', 4) : ($forum['type'] == 'sub' ? str_repeat('&nbsp;', 8) : '')).$forum['name']);
			}
		}
		if($settings['sortids']) {
            $defaultvalue = '';
            $query = DB::query("SELECT typeid, name, special FROM ".DB::table('forum_threadtype')." ORDER BY typeid DESC");
			while($threadtype = DB::fetch($query)) {
				if($threadtype['special']) {
                    if(empty($defaultvalue)) {
                        $defaultvalue = $threadtype['typeid'];
                    }
					$settings['sortids']['value'][] = array($threadtype['typeid'], $threadtype['name']);
				}
			}
            $settings['sortids']['default'] = $defaultvalue;
		}
		return $settings;
	}

	function cookparameter($parameter) {
		return $parameter;
	}

	function getdata($style, $parameter) {
		global $_G;

		$parameter = $this->cookparameter($parameter);

		loadcache('forums');
		$tids		= !empty($parameter['tids']) ? explode(',', $parameter['tids']) : array();
		$fids		= isset($parameter['fids']) && !in_array(0, (array)$parameter['fids']) ? $parameter['fids'] : array_keys($_G['cache']['forums']);
		$startrow	= !empty($parameter['startrow']) ? intval($parameter['startrow']) : 0;
		$items		= !empty($parameter['showitems']) ? intval($parameter['showitems']) : 10;
		$digest		= isset($parameter['digest']) ? $parameter['digest'] : 0;
		$stick		= isset($parameter['stick']) ? $parameter['stick'] : 0;
		$orderby	= isset($parameter['orderby']) ? (in_array($parameter['orderby'],array('lastpost','dateline','replies','views','heats','recommends','hourviews','todayviews','weekviews','monthviews')) ? $parameter['orderby'] : 'lastpost') : 'lastpost';
		$hours		= isset($parameter['hours']) ? intval($parameter['hours']) : 0;
		$recommend	= !empty($parameter['recommend']) ? 1 : 0;
		$sortids	= isset($parameter['sortids']) ? $parameter['sortids'] : '';

        $datalist = $list = array();
		$threadtypeids = array();

		$sql = ($fids ? ' AND t.fid IN ('.dimplode($fids).')' : '')
			.($tids ? ' AND t.tid IN ('.dimplode($tids).')' : '')
			.($sortids ? ' AND t.sortid IN ('.dimplode($sortids).')' : '')
			.($digest ? ' AND t.digest IN ('.dimplode($digest).')' : '')
			.($stick ? ' AND t.displayorder IN ('.dimplode($stick).')' : '')
			." AND t.isgroup='0'";
		if(in_array($orderby, array('hourviews','todayviews','weekviews','monthviews'))) {
			$historytime = 0;
			switch($orderby) {
				case 'hourviews':
					$historytime = TIMESTAMP - 3600 * $hours;
				break;
				case 'todayviews':
					$historytime = mktime(0, 0, 0, date('m', TIMESTAMP), date('d', TIMESTAMP), date('Y', TIMESTAMP));
				break;
				case 'weekviews':
					$week = gmdate('w', TIMESTAMP) - 1;
					$week = $week != -1 ? $week : 6;
					$historytime = mktime(0, 0, 0, date('m', TIMESTAMP), date('d', TIMESTAMP) - $week, date('Y', TIMESTAMP));
				break;
				case 'monthviews':
					$historytime = mktime(0, 0, 0, date('m', TIMESTAMP), 1, date('Y', TIMESTAMP));
				break;
			}
			$sql .= ' AND t.dateline>='.$historytime;
			$orderby = 'views';
		} elseif($orderby == 'heats') {
			$_G['setting']['indexhot']['days'] = !empty($_G['setting']['indexhot']['days']) ? intval($_G['setting']['indexhot']['days']) : 8;
			$heatdateline = TIMESTAMP - 86400 * $_G['setting']['indexhot']['days'];
			$sql .= " AND t.dateline>'$heatdateline' AND t.heats>'0'";
		}
		$sqlfrom = "FROM `".DB::table('forum_thread')."` t";
		if($recommend) {
			$sqlfrom .= " INNER JOIN `".DB::table('forum_forumrecommend')."` fc ON fc.tid=t.tid";
		}

        $html = '';
		$query = DB::query("SELECT t.tid,t.fid,t.readperm,t.author,t.authorid,t.subject,t.dateline,t.lastpost,t.lastposter,t.views,t.replies,t.highlight,t.digest,t.typeid,t.sortid,t.heats,t.recommends
			$sqlfrom WHERE t.readperm='0'
			$sql
			AND t.displayorder>='0'
			ORDER BY t.$orderby DESC
			LIMIT $startrow,$items;"
			);
		while($data = DB::fetch($query)) {
            $html .= $this->showsort($data);
        }
        return array('html' => $html, 'data' => null);
    }

    function showsort($threaddata) {
        global $_G;
        $sortid = intval($threaddata['sortid']);
        $tid = intval($threaddata['tid']);
        loadcache(array('threadsort_option_'.$sortid, 'threadsort_template_'.$sortid));

        $template = $_G['cache']['threadsort_template_'.$sortid]['block'];
        $sortoption = $_G['cache']['threadsort_option_'.$sortid];

        $optiondata = $optionvaluelist = $optiontitlelist = $optionunitlist = $searchtitle = $searchvalue = $searchunit = $typetemplate = array();
        $query = DB::query("SELECT optionid, value FROM ".DB::table('forum_typeoptionvar')." WHERE tid='$tid'");
        while($option = DB::fetch($query)) {
            $optiondata[$option['optionid']] = $option['value'];
        }

        $threaddata['subject'] = '<a href="'.'forum.php?mod=viewthread&tid='.$threaddata['tid'].'" '.$threaddata['highlight'].' target="_blank">'.$threaddata['subject'].'</a>';
        $threaddata['author'] = '<a href="'.'home.php?mod=space&uid='.$threaddata['authorid'].'" target="_blank">'.$threaddata['author'].'</a>';

        if($sortoption && $template && $optiondata && $threaddata) {
            foreach($sortoption as $optionid => $option) {
                $optiontitlelist[] = $sortoption[$optionid]['title'];
                $optionunitlist[] = $sortoption[$optionid]['unit'];
                if($sortoption[$optionid]['type'] == 'checkbox') {
                    $choicedata = '';
                    foreach(explode("\t", $optiondata[$optionid]) as $choiceid) {
                        $choicedata .= '<span>'.$sortoption[$optionid]['choices'][$choiceid].'</span>';
                    }
                    $optionvaluelist[] = $choicedata;
                } elseif(in_array($sortoption[$optionid]['type'], array('radio', 'select'))) {
                    $optionvaluelist[] = $sortoption[$optionid]['choices'][$optiondata[$optionid]];
                } elseif($sortoption[$optionid]['type'] == 'image') {
                    $optionvaluelist[] = $optiondata[$optionid] ? $optiondata[$optionid] : 'static/image/forum/default/nophoto.gif';
                } elseif($sortoption[$optionid]['type'] == 'url') {
                    $optiondata[$optionid] = preg_match('/^(ftp|http|)[s]?:\/\//', $optiondata[$optionid]) ? $optiondata[$optionid] : $optiondata[$optionid];
                    $optionvaluelist[] = $optiondata[$optionid] ? "<a href=\"".$optiondata[$optionid]."\" target=\"_blank\">".$optiondata[$optionid]."</a>" : '';
                } elseif($sortoption[$optionid]['type'] == 'textarea') {
                    $optionvaluelist[] = $optiondata[$optionid] ? nl2br($optiondata[$optionid]) : '';
                } else {
                    $optionvaluelist[] = $optiondata[$optionid] ? $optiondata[$optionid] : $sortoption[$optionid]['defaultvalue'];
                }
            }

            foreach($sortoption as $option) {
                $searchtitle[] = '/{('.$option['identifier'].')}/i';
                $searchvalue[] = '/\[('.$option['identifier'].')value\]/i';
                $searchunit[] = '/\[('.$option['identifier'].')unit\]/i';
            }

            $typetemplate = preg_replace(array("/\{author\}/i", "/\{subject\}/i", "/\{dateline\}/i", "/\{url\}/i", "/\[url\](.+?)\[\/url\]/i"),
                            array(
                                $threaddata['author'],
                                $threaddata['subject'],
                                dgmdate($threaddata['dateline'], 'm-d'),
                                "forum.php?mod=viewthread&tid=$tid",
                                "<a href=\""."forum.php?mod=viewthread&tid=$tid\" target=\"_blank\">\\1</a>"
                            ), stripslashes($template));
            $typetemplate = preg_replace($searchtitle, $optiontitlelist, $typetemplate);
            $typetemplate = preg_replace($searchvalue, $optionvaluelist, $typetemplate);
            $typetemplate = preg_replace($searchunit, $optionunitlist, $typetemplate);
        }

        return $typetemplate;
    }
}


?>